# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _, tools


SQL_SELECT = '''
    SELECT  
        ln.id, ln.fapiao_id,
        ln.id as paper_line_id, dis.id as discount_paper_line_id, 
        ln.create_uid, ln.write_uid, ln.create_date, ln.write_date
    FROM fp_fapiao_paper_line ln
    left join fp_fapiao_paper_line dis 
        on ln.fapiao_id = dis.fapiao_id 
        and ln.row_number = dis.ref_row_number
        and dis.line_type = 'discount'
    WHERE ln.line_type = 'line'
'''


class FapiaoBillLine(models.Model):
    _name = 'fp.bill.line'
    _auto = False
    _description = 'Fapiao Bill Line'
    _order = 'fapiao_id, row_number, id'

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        query = "CREATE OR REPLACE VIEW %s AS (%s)" % (
            self._table, SQL_SELECT)
        self.env.cr.execute(query % ())

    fapiao_id = fields.Many2one(
        'fp.bill', string='Fapiao',
        required=True, ondelete='cascade', index=True, copy=False)

    paper_line_id = fields.Many2one(
        'fp.fapiao.paper.line', string='Paper Line')

    row_number = fields.Integer(related='paper_line_id.row_number')

    discount_paper_line_id = fields.Many2one(
        'fp.fapiao.paper.line', string='Discount Paper Line')

    company_id = fields.Many2one(related='fapiao_id.company_id')
    type = fields.Selection(related='fapiao_id.type')
    invoice_type = fields.Selection(related='fapiao_id.invoice_type')
    state = fields.Selection(related='fapiao_id.state')

    name = fields.Char(related='paper_line_id.name')
    sequence = fields.Integer(related='paper_line_id.sequence')

    product_id = fields.Many2one(related='paper_line_id.product_id')
    vat_product_id = fields.Many2one(
        string='Vat Product', related='product_id.vat_product_id')
    vat_product_code = fields.Char(
        'Vat Product Code', related='product_id.vat_product_code')
    vat_product_name = fields.Char(
        'Vat Product Name', related='product_id.vat_product_name')
    product_code = fields.Char('Product code', related='product_id.code')
    product_name = fields.Char('Product Name', related='product_id.name')
    product_spec = fields.Char('Spec', related='product_id.spec')
    product_uom = fields.Char('Uom', related='product_id.uom')

    tax_ratio = fields.Float(related='paper_line_id.tax_ratio')

    quantity = fields.Float(related='paper_line_id.quantity')
    amount_untaxed = fields.Float(related='paper_line_id.amount_untaxed')
    amount_tax = fields.Float(related='paper_line_id.amount_tax')

    amount_total = fields.Float(related='paper_line_id.amount_total')
    price_untax = fields.Float(related='paper_line_id.price_untax')
    price_unit = fields.Float(related='paper_line_id.price_unit')

    amount_discount = fields.Float('Discount', compute='_compute_discount')
    amount_untaxed_discount = fields.Float(
        'Untaxed Discount Amount', compute='_compute_discount')
    amount_tax_discount = fields.Float(
        'Tax Discount', compute='_compute_discount')
    discount_row_number = fields.Integer(
        'Discount Row Number', compute='_compute_discount')

    @api.depends('discount_paper_line_id')
    def _compute_discount(self):
        for line in self:
            dis = line.discount_paper_line_id
            line.discount_row_number = dis.row_number or 0
            line.amount_discount = dis.amount_total or 0.0
            line.amount_untaxed_discount = dis.amount_untaxed or 0.0
            line.amount_tax_discount = dis.amount_tax or 0.0
